
global path "/Users/PSG24/Dropbox/Papers/GGP_Brokers/"
global path "/gpfs/home/psg24/GGP_Brokers/"
estread using $path/output/tables/regressions

local keep_vars_sub endog_exp endog_exp_bust endog_exp_medium
local keep_vars_all endog_exp_all endog_exp_bust_all endog_exp_medium_all

foreach outcome in  sale365 will_fc days_to_sale  dom_ggp log_list log_close discount linfer_price linfer_diff loginfer_hp_sale_diff discount2   sale30 sale90 sale180 sale365_indiv buy_commission_cens {
	if "`outcome'" == "sale365_indiv" {
            local endog_exp  "endog_exp_all"
            local endog_exp_bust  "endog_exp_bust_all"
            local endog_exp_boom  "endog_exp_boom_all"
            local endog_exp_medium  "endog_exp_medium_all"
            local keep_vars  `keep_vars_all'
	}
    else   {
            local endog_exp  "endog_exp"
            local endog_exp_bust  "endog_exp_bust"
            local endog_exp_boom  "endog_exp_boom"
            local endog_exp_medium  "endog_exp_medium"
            local keep_vars  `keep_vars_sub'            
            }
	estimates restore ols1_`outcome'
	estimates restore ols2_`outcome'
	reghdfe
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[`endog_exp'] + _b[`endog_exp_medium']'
	estimates restore ols3_`outcome'
	reghdfe	
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[`endog_exp'] + _b[`endog_exp_medium']'

	estimates restore iv1_`outcome'
	estimates restore iv2_`outcome'
	ivreghdfe	
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig=r(p)
	estadd scalar bust_eff=`=_b[`endog_exp'] + _b[`endog_exp_bust']'


	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig=r(p)
	estadd scalar medium_eff=`=_b[`endog_exp'] + _b[`endog_exp_medium']'
	estimates store iv2_`outcome'	
	estimates restore iv3_`outcome'
	reghdfe		
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig=r(p)
	estadd scalar bust_eff=`=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig=r(p)
	estadd scalar medium_eff=`=_b[`endog_exp'] + _b[`endog_exp_medium']'
	estimates store iv3_`outcome'	


	esttab ols1_`outcome' ols2_`outcome' ols3_`outcome'  iv2_`outcome' iv3_`outcome',    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(`endog_exp' "Log(Exp + 1)" `endog_exp_bust' "Bust $\times$ Log(Exp + 1)" `endog_exp_medium' "Medium $\times$ Log(Exp + 1)")
	esttab ols1_`outcome' ols2_`outcome' ols3_`outcome'  iv2_`outcome' iv3_`outcome' using $path/output/tables/`outcome'_table_iv.tex, frag booktabs replace   obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(`endog_exp' "Log(Exp + 1)" `endog_exp_bust' "Bust $\times$ Log(Exp + 1)" `endog_exp_medium' "Medium $\times$ Log(Exp + 1)")

	estimates restore sub1_`outcome'
	reghdfe
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[`endog_exp'] + _b[`endog_exp_medium']'
	estimates restore sub2_`outcome'
	reghdfe
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[`endog_exp'] + _b[`endog_exp_medium']'
	estimates restore sub3_`outcome'
	reghdfe	
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[`endog_exp'] + _b[`endog_exp_medium']'

	estimates restore iv4_`outcome'
	reghdfe		
	test `endog_exp' + `endog_exp_bust' = 0
	estadd scalar bust_sig=r(p)
	estadd scalar bust_eff=`=_b[`endog_exp'] + _b[`endog_exp_bust']'
	test `endog_exp' + `endog_exp_medium' = 0
	estadd scalar medium_sig=r(p)
	estadd scalar medium_eff=`=_b[`endog_exp'] + _b[`endog_exp_medium']'

	}

local keep_vars endog_exp endog_exp_bust endog_exp_medium

esttab ols3_linfer_diff   ols3_loginfer_hp_sale_diff  ols3_discount2 iv2_linfer_diff iv2_loginfer_hp_sale_diff iv2_discount2  using $path/output/tables/price_table.tex, booktabs replace frag  obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(endog_exp "Log(Exp + 1)" endog_exp_bust "Bust $\times$ Log(Exp + 1)" endog_exp_medium "Medium $\times$ Log(Exp + 1)")

esttab ols3_linfer_diff_indiv   ols3_linfer_sale_diff_indiv  ols3_discount2_indiv iv2_linfer_diff_indiv iv2_linfer_sale_diff_indiv iv2_discount2_indiv  using $path/output/tables/price_table_indiv.tex, booktabs replace frag  obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars_all') interaction(" $\times$ ") label coeflabels(endog_exp_all "Log(Exp + 1)" endog_exp_bust_all "Bust $\times$ Log(Exp + 1)" endog_exp_medium_all "Medium $\times$ Log(Exp + 1)")

esttab iv4_sale365 iv4_will_fc iv4_linfer_diff  iv4_loginfer_hp_sale_diff  iv4_discount2  using $path/output/tables/complier_table.tex, booktabs replace frag    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "infer_hp Inferred House Price" "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(endog_exp "Log(Exp + 1)" endog_exp_bust "Bust $\times$ Log(Exp + 1)" endog_exp_medium "Medium $\times$ Log(Exp + 1)")

esttab iv4_sale365_indiv iv4_will_fc_indiv iv4_linfer_diff_indiv  iv4_linfer_sale_diff_indiv  iv4_discount2_indiv  using $path/output/tables/complier_table_indiv.tex, booktabs replace frag    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "infer_hp Inferred House Price" "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars_all') interaction(" $\times$ ") label coeflabels(endog_exp_all "Log(Exp + 1)" endog_exp_bust_all "Bust $\times$ Log(Exp + 1)" endog_exp_medium_all "Medium $\times$ Log(Exp + 1)")


esttab sub2_sale365 sub2_will_fc sub2_linfer_diff sub2_loginfer_hp_sale_diff sub2_discount2  using $path/output/tables/infer_price_table.tex, booktabs replace fra    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "infer_hp Inferred House Price" "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(endog_exp "Log(Exp + 1)" endog_exp_bust "Bust $\times$ Log(Exp + 1)" endog_exp_medium "Medium $\times$ Log(Exp + 1)")

esttab sub3_sale365 sub3_will_fc sub3_linfer_diff sub3_loginfer_hp_sale_diff sub3_discount2 using $path/output/tables/equity_stake_table.tex, booktabs replace fra   obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "equity_stake Equity Stake" "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(endog_exp "Log(Exp + 1)" endog_exp_bust "Bust $\times$ Log(Exp + 1)" endog_exp_medium "Medium $\times$ Log(Exp + 1)")	

foreach outcome in sale365 will_fc linfer_diff  loginfer_hp_sale_diff discount  {
	estimates restore agent_`outcome'
	reghdfe
	test endog_exp + endog_exp_bust = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[endog_exp] + _b[endog_exp_bust]'
	test endog_exp + endog_exp_medium = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[endog_exp] + _b[endog_exp_medium]'
	}
esttab agent_sale365 agent_will_fc  agent_linfer_diff agent_loginfer_hp_sale_diff agent_discount  using $path/output/tables/summary_table_agent_fe.tex, booktabs replace frag    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "agent_fe Agent FE" "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(`keep_vars') interaction(" $\times$ ") label coeflabels(endog_exp "Log(Exp + 1)" endog_exp_bust "Bust $\times$ Log(Exp + 1)" endog_exp_medium "Medium $\times$ Log(Exp + 1)") 


estimates restore iv2_sale365_w_firststage
ivreghdfe
mat first = e(first)

local F_base = first["F", "endog_exp"]
local F_bust = first["F", "endog_exp_bust"]
local F_medium = first["F", "endog_exp_medium"]

estimates restore st1endog_exp
ivreg2
estadd scalar F = `F_base'
estimates store st1endog_exp
estimates restore st1endog_exp_bust
ivreg2
estadd scalar F = `F_bust'
estimates store st1endog_exp_bust
estimates restore st1endog_exp_medium
ivreg2
estadd scalar F = `F_medium'
estimates store st1endog_exp_medium

esttab st1* ,  obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalar("F First-stage F-statistic"  )   keep(Z Z_bust Z_medium) interaction(" $\times$ ") label coeflabels(Z "Log(Buyer Agent Exp + 1) $\times$ Inactive" Z_bust "Bust $\times$ Log(Buyer Agent Exp + 1) $\times$ Inactive" Z_medium "Medium $\times$ Log(Buyer Agent Exp + 1) $\times$ Inactive") 

esttab st1* using $path/output/tables/first_stage.tex, booktabs replace frag   obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("F First-stage F-statistic" )   keep(Z Z_bust Z_medium) interaction(" $\times$ ") label coeflabels(Z "Log(Buyer Agent Exp + 1) $\times$ Inactive" Z_bust "Bust $\times$ Log(Buyer Agent Exp + 1) $\times$ Inactive" Z_medium "Medium $\times$ Log(Buyer Agent Exp + 1) $\times$ Inactive") 


foreach outcome in sale365 will_fc linfer_diff  loginfer_hp_sale_diff discount2  {
	estimates restore dth3_`outcome'
	reghdfe
	test logexp + 1.period#c.logexp = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[logexp] + _b[1.period#c.logexp]'
	test logexp + 2.period#c.logexp = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[logexp] + _b[2.period#c.logexp]'
	}
esttab dth3_sale365 dth3_will_fc  dth3_linfer_diff dth3_loginfer_hp_sale_diff dth3_discount2  using $path/output/tables/summary_table_death.tex, booktabs replace frag    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")   keep(logexp 1.period#c.logexp 2.period#c.logexp) interaction(" $\times$ ") label coeflabels(logexp "Log(Exp + 1)"  1.period#c.logexp "Bust $\times$ Log(Exp + 1)" 2.period#c.logexp "Medium $\times$ Log(Exp + 1)") 


foreach outcome in sale365 will_fc linfer_diff  loginfer_hp_sale_diff discount2  {
	estimates restore cv3_`outcome'
	reghdfe
	test logexp + 1.period#c.logexp = 0
	estadd scalar bust_sig = r(p)
	estadd scalar bust_eff = `=_b[logexp] + _b[1.period#c.logexp]'
	test logexp + 2.period#c.logexp = 0
	estadd scalar medium_sig = r(p)
	estadd scalar medium_eff = `=_b[logexp] + _b[2.period#c.logexp]'
	}
esttab cv3_sale365 cv3_will_fc  cv3_linfer_diff cv3_loginfer_hp_sale_diff cv3_discount2  using $path/output/tables/summary_table_CV.tex, booktabs replace frag    obslast b(%05.4f) se(%05.4f)  star(* 0.1  ** .05  *** 0.01) nomtitles nonotes   scalars("zip_time_fe Time-by-Zipcode FE" "house_char_fe House Char." "bust_eff Bust Effect" "bust_sig Bust p-value" "medium_eff Medium Effect" "medium_sig Medium p-value")    keep(logexp 1.period#c.logexp 2.period#c.logexp) interaction(" $\times$ ") label coeflabels(logexp "Log(Exp + 1)"  1.period#c.logexp "Bust $\times$ Log(Exp + 1)" 2.period#c.logexp "Medium $\times$ Log(Exp + 1)") 
